<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

$_current_dir = dirname(__FILE__) . '/';
require($_current_dir . 'includes/application_top.php');
//
include(CONFIG_PATH . 'lib/xajax_0.2.4/xajax.inc.php');
//
$action = trim(strtolower(util_get_param($_GET, 'action')));
$page = trim(strtolower(util_get_param($_GET, 'page')));
if( !is_numeric($page) ) {
	$page = 1;
}

///////////////////////////////////////////////////////////////////////////////
//
function _alert_auth() {
	$objResponse = new xajaxResponse();
	$msg = "You dont have permission!";
	$objResponse->addAlert($msg);
	
	return $objResponse;
}

function _alert_message($msg) {
	$objResponse = new xajaxResponse();
	$objResponse->addAlert($msg);
	
	return $objResponse;
}

function _script_cb($call, $result) {
	$objResponse = new xajaxResponse();
	$objResponse->addScriptCall($call, $result);
	
	return $objResponse;
}

function _check_auth($type = 'admin') {
	if( $type == 'admin' ) {
		if( $_SESSION['suser']->userid > 0 && $_SESSION['suser']->usertype == 'admin' ) {
			return true;
		}
	} elseif( $type == 'member' ) {
		if( $_SESSION['suser']->userid > 0 && in_array($_SESSION['suser']->usertype, array('member', 'admin')) ) {
			return true;
		}
	} else {
		if( $_SESSION['suser']->userid == 0 ) {
			return true;
		}
	}
	
	return false;
}

///////////////////////////////////////////////////////////////////////////////
//FRONTS
///////////////////////////////////////////////////////////////////////////////
function ws_contactus($data_array) {
	//
	if( isset($data_array['checkcode']) && isset($_SESSION['checkcode']) ) {
		if( $data_array['checkcode'] !== $_SESSION['checkcode'] ) {
			return 	_alert_message(_lang('ws:error_check_code'));
		}
	}
	//
	$data_array = util_htmlfilter($data_array);
	if( isset($data_array['field_content']) ) {
		$data_array['field_content'] = util_substr($data_array['field_content'], 0, 4000);
	}
	//
	include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/contactus/contactus.class.php');
	$mObj = new ContactusBase();
	//
	$id = $mObj->addItem($data_array);
	//
	$objResponse = new xajaxResponse();
	$r = 0;
	if( $id > 0 ) {
		$r = 1;	
	}
	$objResponse->addScriptCall("ws_contactus_cb", $r);
	
	//
	//MAIL
	ob_start();
	//
	include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'mail.class.php');
	$mailObj = new MailBase();
	$mailObj->mailsendtype = 'send';
	
	$email_subject = 'Contact Us Message';
	$email_text = '';
	foreach($data_array as $k => $v) {
		if( substr($k, 0, strlen('field_')) == 'field_' ) {
			$sep = "<br>";
			$k = str_replace('_', ' ', substr($k, strlen('field_')));
			if( $k == 'sizewd' ) {
				$k = 'Size of Windows or Doors';
				$sep .= "<br>";
			} elseif( $k == 'comment' ) {
				$sep .= "<br>";
			} elseif( $k == 'image' ) {
				$k = 'Picture';
				$v = '<a href="' . CONFIG_URL_HTTP . CONFIG_DIR_UPLOADS_ORIGINS . $v . '">' . $v . '</a>';
			}
			$email_text .= ucfirst($k) . ': ' . $v . $sep;
		}
	}
	$mailObj->sendToSystem($email_subject, $email_text);
	$_CONTENT = ob_get_contents();
	ob_clean();
	//
	
	return $objResponse;
}

///////////////////////////////////////////////////////////////////////////////
function ws_postform($data_array, $f = '') {
	$data_array = util_htmlfilter($data_array);
	if( isset($data_array['field_content']) ) {
		$data_array['field_content'] = util_substr($data_array['field_content'], 0, 4000);
	}
	$form = $data_array['form'];
	
	//
	if( isset($data_array['checkcode']) && isset($_SESSION['checkcode']) ) {
		if( $data_array['checkcode'] !== $_SESSION['checkcode'] ) {
			return 	_alert_message(_lang('ws:error_check_code'));
		}
	}
	
	//
	if( $form == 'announcement_register' ) {
		$target = $data_array['target'];
		
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/announcement/announcement.register.class.php');
		$mObj = new AnnouncementRegisterBase();
		$mObj->setOwner($target);
		
		$items_id = $mObj->addItem($data_array);
	} elseif( $form == 'content_comment' ) {
		$target = $data_array['target'];
		
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/content/content.comment.class.php');
		$mObj = new ContentCommentBase();
		$mObj->setOwner($target);
		
		$items_id = $mObj->addItem($data_array);
	} elseif( $form == 'news_comment' ) {
		$target = $data_array['target'];
		
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/news/news.comment.class.php');
		$mObj = new NewsCommentBase();
		$mObj->setOwner($target);
		
		$items_id = $mObj->addItem($data_array);
	} elseif( $form == 'page_comment' ) {
		$target = $data_array['target'];
		
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/page/page.comment.class.php');
		$mObj = new PageCommentBase();
		$mObj->setOwner($target);
		
		$items_id = $mObj->addItem($data_array);
	} elseif( $form == 'dform' ) {
		$fid = $data_array['id'];
		
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/form/form.data.class.php');
		$mObj = new FormDataBase();
		
		$items_id = $mObj->addFormData($fid, $data_array);
	}
	
	//
	$r = array();
	$r['f'] = $f;
	if( $items_id > 0 ) {
		$r['status'] = 1;	
	} else {
		$r['status'] = 0;	
	}
	
	$objResponse = new xajaxResponse();
	$objResponse->addScriptCall("pjs_ws.postFormCB", $r);
	
	return $objResponse;
}

///////////////////////////////////////////////////////////////////////////////
function ws_content_edit($id, $value, $type = 'content') {
	include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'user.session.class.php');
	$usObj = new UserSessionBase();
	$check = $usObj->checkAdmin($_REQUEST['PPSIDA']);
	if( $check == false ) {
		return _alert_auth();
	}
	
	//
	include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/content/content.class.php');
	$mObj = new ContentBase();
	$mObj->format = true;
	
	if( $type == 'subject' ) {
		$data_array['_fulltext'] = $value;
	} elseif( $type == 'introtext' || $type == 'brief' ) {
		$data_array['_fulltext'] = $value;
	} elseif( $type == 'fulltext' || $type == 'content' ) {
		$data_array['_fulltext'] = $value;
	}
	$mObj->updateItem($id, $data_array);
	
	$r = 1;
	
	//
	$objResponse = new xajaxResponse();
	$objResponse->addScriptCall("content_edit_savecb", $r);
	
	return $objResponse;
}

///////////////////////////////////////////////////////////////////////////////
//ADMINS
///////////////////////////////////////////////////////////////////////////////
function ws_selectimage($id) {	
	//
	include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/gallery/gallery.image.class.php');
	$mObj = new GalleryImageBase();
	
	$item = $mObj->getItem($id);
	//
	$objResponse = new xajaxResponse();
	$objResponse->addAssign("gaimage", "innerHTML", "");
	
	return $objResponse;
}

///////////////////////////////////////////////////////////////////////////////
// Instantiate the xajax object.  No parameters defaults requestURI to this page, method to POST, and debug to off
$xajax = new xajax();

//$xajax->debugOn(); // Uncomment this line to turn debugging on

// Specify the PHP functions to wrap. The JavaScript wrappers will be named xajax_functionname
//
//
$xajax->registerFunction("ws_contactus");

//
$xajax->registerFunction("ws_postform");

//
$xajax->registerFunction("ws_content_edit");

//
$xajax->registerFunction("ws_selectimage");

// Process any requests.  Because our requestURI is the same as our html page,
// this must be called before any headers or HTML output have been sent
$xajax->processRequests();
//
?>